home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Utilities / Cron ƒ / cron commons / argc Receiver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-06  |  7.9 KB  |  428 lines  |  [TEXT/KAHL]

  1. /*    ——————————————————————————————————————————————————————————————————
  2.     
  3.     Filename: "argc Receiver.c"
  4.     argc/argv argument receiver code, written for THINK C 5.0
  5.     By Chris Johnson
  6.     Version of: Thursday, July 23, 1992
  7.     
  8.     Distribute freely and without charge, but say something nice about 
  9.     the author when you use it.  Please send me a copy of any improve-
  10.     ments you make so they can be incorporated into future versions.
  11.     
  12.     ——————————————————————————————————————————————————————————————————
  13.     Internet:    chrisj@emx.utexas.edu
  14.     UUCP:        {husc6|uunet}!cs.utexas.edu!ut-emx!chrisj
  15.     BitNet:        chrisj@utxvm.bitnet
  16.     AppleLink:    chrisj@emx.utexas.edu@internet#
  17.     CompuServe:    >INTERNET:chrisj@emx.utexas.edu
  18.     US Mail:    Chris Johnson, 3311 Red River #305, Austin, TX 78705
  19.     ——————————————————————————————————————————————————————————————————    */
  20.  
  21.  
  22. #include "argc Receiver.h"
  23. #include <AppleEvents.h>
  24. #include <GestaltEqu.h>
  25. #include <Traps.h>
  26.  
  27.  
  28. #define argcEventClass        'args'
  29. #define argcEventID            'argc'
  30. #define argcKeyword            'argc'
  31. #define LBitMask(BitNum)    (0x1L << (BitNum))
  32.  
  33.  
  34. static Boolean            QuitFlag;
  35. static Boolean            WaitFlag;
  36. static int                argc;
  37. static char                **argv;
  38.  
  39.  
  40. Boolean                    argInit(void);
  41. void                    argExit(void);
  42. void                    argGet(int *argc, char ***argv);
  43.  
  44. Boolean                    EnvironmentCheck(void);
  45. Boolean                    TrapAvailable(short Trap);
  46.  
  47. pascal OSErr            Quit(AppleEvent *RcvdEvent, AppleEvent *ReplyEvent, long RefCon);
  48. pascal OSErr            ArgsGet(AppleEvent *RcvdEvent, AppleEvent *ReplyEvent, long RefCon);
  49. void                    ArgsDispose(void);
  50. OSErr                    MyGotRequiredParams(AppleEvent *Event);
  51.  
  52. short                    NumToolboxTraps(void);
  53. TrapType                GetTrapType(short theTrap);
  54.  
  55. void                    MacInits(void);
  56. Boolean                    BackgroundOnlyApp(void);
  57.  
  58.  
  59.  
  60.  
  61. #ifdef ReinvokeMain
  62.  
  63. void main() {
  64.     
  65.     argInit();
  66.     
  67.     while (TRUE) {
  68.         int                        argcRcvd;
  69.         char                    **argvRcvd;
  70.         
  71.         argGet(&argcRcvd, &argvRcvd);
  72.         mainarg(argcRcvd, argvRcvd);
  73.     }
  74. }
  75.  
  76. #else
  77.  
  78. void argcReceiver(argcRcvd, argvRcvd)
  79. int                        *argcRcvd;
  80. char                    ***argvRcvd;
  81. {
  82.     if (argInit())        
  83.         argGet(argcRcvd, argvRcvd);
  84. }
  85.  
  86. #endif
  87.  
  88.  
  89. static Boolean argInit() {
  90.     Boolean                    Continue;
  91.     
  92.     Continue = FALSE;
  93.     
  94.     MacInits();
  95.     
  96.     if (EnvironmentCheck())
  97.         Continue = TRUE;
  98.     
  99.     return (Continue);
  100. }
  101.  
  102.  
  103. static void argExit() {
  104.     
  105.     ArgsDispose();
  106.     
  107.     ExitToShell();
  108. }
  109.  
  110.  
  111. static void argGet(argcRcvd, argvRcvd)
  112. int                        *argcRcvd;
  113. char                    ***argvRcvd;
  114. {
  115.     ArgsDispose();
  116.     
  117.     if (QuitFlag == FALSE) {
  118.         unsigned long            TimeoutTicks;
  119.         
  120.         WaitFlag = TRUE;
  121.         TimeoutTicks = (unsigned long) Ticks + 15 * 60;
  122.         
  123.         while (WaitFlag && ((unsigned long) Ticks < TimeoutTicks)) {
  124.             EventRecord                Event;
  125.             
  126.             if (WaitNextEvent(everyEvent, &Event, 60, NULL)) {
  127.                 
  128.                 switch (Event.what) {
  129.                         
  130.                     case keyDown:
  131.                         
  132.                         if ((Event.modifiers & cmdKey) != 0) {
  133.                             
  134.                             if ((unsigned char) Event.message == 'q') {
  135.                                 
  136.                                 QuitFlag = TRUE;
  137.                                 WaitFlag = FALSE;
  138.                             }
  139.                         }
  140.                         break;
  141.                         
  142.                     case kHighLevelEvent:
  143.                         AEProcessAppleEvent(&Event);
  144.                         break;
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     
  150.     if (argv == NULL)
  151.         argExit();
  152.     
  153.     *argcRcvd = argc;
  154.     *argvRcvd = argv;
  155. }
  156.  
  157.  
  158. static Boolean EnvironmentCheck() {
  159.     static Boolean            Inited = FALSE;
  160.     
  161.     if (Inited == FALSE) {
  162.         
  163.         if (TrapAvailable(_WaitNextEvent)) {
  164.             
  165.             if (TrapAvailable(_GestaltDispatch)) {
  166.                 long                    Response;
  167.                 
  168.                 if (Gestalt(gestaltAppleEventsAttr, &Response) == noErr) {
  169.                     
  170.                     if ((Response & LBitMask(gestaltAppleEventsPresent)) != 0) {
  171.                         
  172.                         if (AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, Quit, 0, FALSE) == noErr) {
  173.                             
  174.                             if (AEInstallEventHandler(argcEventClass, argcEventID, ArgsGet, 0, FALSE) == noErr) {
  175.                                 
  176.                                 QuitFlag = FALSE;
  177.                                 WaitFlag = TRUE;
  178.                                 
  179.                                 Inited = TRUE;
  180.                             }
  181.                         }
  182.                     }
  183.                 }
  184.             }
  185.         }
  186.     }
  187.     
  188.     return (Inited);
  189. }
  190.  
  191.  
  192. static pascal OSErr Quit(RcvdEvent, ReplyEvent, RefCon)
  193. AppleEvent                *RcvdEvent;
  194. AppleEvent                *ReplyEvent;
  195. long                    RefCon;
  196. {
  197.     OSErr                    OSError;
  198.     
  199.     OSError = MyGotRequiredParams(RcvdEvent);
  200.     if (OSError == noErr) {
  201.         
  202.         QuitFlag = TRUE;
  203.         WaitFlag = FALSE;
  204.     }
  205.     
  206.     return (OSError);
  207. }
  208.  
  209.  
  210. static pascal OSErr ArgsGet(RcvdEvent, ReplyEvent, RefCon)
  211. AppleEvent                *RcvdEvent;
  212. AppleEvent                *ReplyEvent;
  213. long                    RefCon;
  214. {
  215.     OSErr                    OSError;
  216.     AEDesc                    ArgCList;
  217.     
  218.     OSError = AEGetParamDesc(RcvdEvent, argcKeyword, typeAEList, &ArgCList);
  219.     if (OSError == noErr) {
  220.         long                    ArgCount;
  221.         
  222.         OSError = AECountItems(&ArgCList, &ArgCount);
  223.         if (OSError == noErr) {
  224.             
  225.             argc = ArgCount;
  226.             argv = (char **) NewPtrClear(sizeof(Ptr) * (ArgCount + 1));
  227.                     
  228.             OSError = MemError();
  229.             if (OSError == noErr) {
  230.                 long                    Index;
  231.                 char                    **CurArgV;
  232.                 
  233.                 Index = 0;
  234.                 CurArgV = argv;
  235.                 
  236.                 while (--ArgCount >= 0 && OSError == noErr) {
  237.                     DescType                DataType;
  238.                     long                    DataSize;
  239.                     
  240.                     OSError = AESizeOfNthItem(&ArgCList, ++Index, &DataType, &DataSize);
  241.                     if (OSError == noErr) {
  242.                         
  243.                         *CurArgV = (char *) NewPtr(DataSize + 1);
  244.                         OSError = MemError();
  245.                         if (OSError == noErr) {
  246.                             AEKeyword                Keyword;
  247.                             long                    ActualSize;
  248.                             
  249.                             OSError = AEGetNthPtr(&ArgCList, Index, typeChar, &Keyword, &DataType, (Ptr) *CurArgV, DataSize, &ActualSize);
  250.                             if (OSError == noErr)
  251.                                 *(*CurArgV + DataSize) = '\0';
  252.                         }
  253.                     }
  254.                     
  255.                     CurArgV++;
  256.                 }
  257.                 
  258.             }
  259.         }
  260.         
  261.         AEDisposeDesc(&ArgCList);
  262.     }
  263.     
  264.     if (OSError == noErr)
  265.         OSError = MyGotRequiredParams(RcvdEvent);
  266.     
  267.     if (OSError != noErr) {
  268.         
  269.         argc = 0;
  270.         argv = NULL;
  271.     }
  272.     
  273.     WaitFlag = FALSE;
  274.     
  275.     return (OSError);
  276. }
  277.  
  278.  
  279. static OSErr MyGotRequiredParams(Event)
  280. AppleEvent                *Event;
  281. {
  282.     OSErr                    OSError;
  283.     DescType                ReturnedType;
  284.     Size                    ActualSize;
  285.     
  286.     OSError = AEGetAttributePtr(Event, keyMissedKeywordAttr, typeWildCard, &ReturnedType, NULL, 0, &ActualSize);
  287.     if (OSError == errAEDescNotFound) {
  288.         
  289.         OSError = noErr;
  290.         
  291.     } else if (OSError == noErr)
  292.         OSError = errAEEventNotHandled;
  293.     
  294.     return (OSError);
  295. }
  296.  
  297.  
  298. static void ArgsDispose() {
  299.     
  300.     if (argv != NULL) {
  301.         char                    **CurArg;
  302.         
  303.         CurArg = argv;
  304.         
  305.         while (--argc >= 0) {
  306.             
  307.             if (*CurArg != NULL)
  308.                 DisposPtr((Ptr) *CurArg);
  309.                 
  310.             CurArg++;
  311.         }
  312.         
  313.         DisposPtr((Ptr) argv);
  314.     }
  315.     
  316.     argc = 0;
  317.     argv = NULL;
  318. }
  319.  
  320.  
  321. /*    ——————————————————————————————————————————————————————————
  322.     All of the following code comes from IM VI pg. 3-8.
  323.     ——————————————————————————————————————————————————————————    */
  324.  
  325.  
  326.  
  327.  
  328. static short NumToolboxTraps() {
  329.     short                    NumToolboxTraps;
  330.     
  331.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  332.         NumToolboxTraps = 0x200;
  333.     else
  334.         NumToolboxTraps = 0x400;
  335.         
  336.     return (NumToolboxTraps);
  337. }
  338.  
  339.  
  340. static TrapType GetTrapType(theTrap)
  341. short                    theTrap;
  342. {
  343.     TrapType                tType;
  344.     
  345.     if ((theTrap & 0x0800) > 0)
  346.         tType = ToolTrap;
  347.     else
  348.         tType = OSTrap;
  349.     
  350.     return (tType);
  351. }
  352.  
  353.  
  354. Boolean TrapAvailable(theTrap)
  355. short                    theTrap;
  356. {
  357.     TrapType                tType;
  358.     
  359.     tType = GetTrapType(theTrap);
  360.     if (tType == ToolTrap) {
  361.         
  362.         theTrap &= 0x07FF;
  363.         if (theTrap >= NumToolboxTraps())
  364.             theTrap = _Unimplemented;
  365.     }
  366.     
  367.     return (NGetTrapAddress(theTrap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  368. }
  369.  
  370.  
  371. /*    ——————————————————————————————————————————————————————————
  372.     Code to determine what sort of toolbox initializations we
  373.     need to perform.
  374.     ——————————————————————————————————————————————————————————    */
  375.  
  376.  
  377. #define BackgroundOnlyMask        0x0400
  378.  
  379.  
  380. static void MacInits() {
  381.     static Boolean            MacInited = FALSE;
  382.     
  383.     if (MacInited == FALSE) {
  384.         
  385.         InitGraf((Ptr) &thePort);
  386.         
  387.         if (BackgroundOnlyApp() == FALSE) {
  388.             
  389.             InitFonts();
  390.             InitWindows();
  391.             InitMenus();
  392.             TEInit();
  393.             InitDialogs(NULL);
  394.             InitCursor();
  395.         }
  396.         
  397.         MacInited = TRUE;
  398.     }
  399. }
  400.  
  401.  
  402. static Boolean BackgroundOnlyApp() {
  403.     Boolean                    BackgroundOnly;
  404.     Handle                    SizeHand;
  405.     
  406.     BackgroundOnly = FALSE;
  407.     
  408.     SizeHand = Get1Resource('SIZE', -1);
  409.     if (SizeHand != NULL) {
  410.         
  411.         LoadResource(SizeHand);
  412.         if (ResError() == noErr) {
  413.             short                    Flags;
  414.             
  415.             Flags = **(short **) SizeHand;
  416.             
  417.             if ((Flags & BackgroundOnlyMask) != 0) {
  418.                 
  419.                 BackgroundOnly = TRUE;
  420.             }
  421.         }
  422.         
  423.         ReleaseResource(SizeHand);
  424.     }
  425.     
  426.     return (BackgroundOnly);
  427. }
  428.